import {Obj_class} from "./models/obj_class";
import {Basebio} from "./models/basebio";


let bloodeffictindex:number=0;
let bloddeffictimg=new Image();
bloddeffictimg.src=require('./images/bloodeffic1.png');

export const allobjects:Obj_class[]=[];
export const enemyenablelimit=1000; //当前屏幕支持的地方活动单位
export const armylimit:number=100; //我方单位数量上线
export const playerteam=0;    //玩家队伍索引


export const drawfirebloodeffic=(ptctx:any,mouse_x:number,mouse_y:number,obj_x:number,obj_y:number,obj_width:number,obj_height:number)=>
{
        if (mouse_x+ 10 >= obj_x && mouse_x + 10 <= obj_x + obj_width+10) {
                if (mouse_y + 10 > obj_y && mouse_y + 10 <= obj_y + obj_height+20) {
                        //血溅效果
                        if(bloodeffictindex==0)
                        {
                                bloddeffictimg.src=require('./images/bloodeffic1.png')
                        }
                        else if(bloodeffictindex==1)
                        {
                                bloddeffictimg.src=require('./images/bloodeffic2.png')
                        }
                        else
                        {
                                bloodeffictindex=0;
                                bloddeffictimg.src=require('./images/bloodeffic3.png')
                        }
                        bloodeffictindex=bloodeffictindex+1;
                        ptctx.drawImage(bloddeffictimg,0,0,bloddeffictimg.width,bloddeffictimg.height,mouse_x-20,mouse_y-20,25,25);
               }
       }
}

export const getrandomaction=():string=> {

        // 0-100
        let tempnum: number = Math.floor(Math.random() * (89 - 0 + 1)) + 0;

        if (tempnum >= 15 && tempnum <= 35) {
                return "d";
        } else if (tempnum >= 36 && tempnum <= 56) {
                return "u";
        }
        // else if (tempnum >= 50 && tempnum <= 55) {
        //         return "l";
        // }
        // else if (tempnum >= 60 && tempnum <= 65)
        // {
        //         return "r"
        // }
        else  if (tempnum >= 80 && tempnum <= 82)
        {
                return "fire";
        }
        else
        {
             return   "d";
        }
}

export const getXY=(canvas:any, evt:any)=>{
        let style = window.getComputedStyle(canvas, null);
        //宽高
        let cssWidth = parseFloat(style["width"]);
        let cssHeight = parseFloat(style["height"]);
        //各个方向的边框长度
        // @ts-ignore
        let borderLeft = parseFloat(style["border-left-width"]);
        // @ts-ignore
        let borderTop = parseFloat(style["border-top-width"]);
        // @ts-ignore
        let paddingLeft = parseFloat(style["padding-left"]);
        // @ts-ignore
        let paddingTop = parseFloat(style["padding-top"]);

        let scaleX = canvas.width / cssWidth; // 水平方向的缩放因子
        let scaleY = canvas.height / cssHeight; // 垂直方向的缩放因子

        let x = evt.clientX;
        let y = evt.clientY;

        let rect = canvas.getBoundingClientRect();
        x -= (rect.left + borderLeft + paddingLeft); // 去除 borderLeft paddingLeft 后的坐标
        y -= (rect.top + borderTop + paddingTop); // 去除 borderLeft paddingLeft 后的坐标
        x *= scaleX; // 修正水平方向的坐标
        y *= scaleY; // 修正垂直方向的坐标
        return {x,y}
}

//Y轴排序
export const  ReStortObjects= (objects:Obj_class[])=> {
        let tempobj;
        for (let i = 0; i < objects.length - 1; i++) {
                for (let j = 1; j < objects.length - i - 1; j++) {
                        if (objects[j - 1].y > objects[j].y) {
                                tempobj = objects[j - 1];
                                objects[j - 1] = objects[j];
                                objects[j] = tempobj;
                        }
                }
        }
}

//资源加载

//资源释放
export const ReleaseResource=(obj:Basebio)=> {
        try {
                for (let i = 0; i < allobjects.length; i++) {
                        if (allobjects[i].objval.name == obj.name) {
                                allobjects.splice(i, 1);
                                return;
                        }
                }
        }
        catch (e) {

        }
}

export const ReleaseAllDestroidResource=()=> {
        try {
                for (let i = 0; i < allobjects.length; i++) {
                        let bisobj:Basebio=allobjects[i].objval;
                        if (bisobj.live<=0 && bisobj.deadfinish==true) {
                                allobjects.splice(i, 1);
                                break;
                        }
                }

                let exitflag:boolean=false;
                for (let i = 0; i < allobjects.length; i++) {
                        let bisobj:Basebio=allobjects[i].objval;
                        if (bisobj.live<=0 && bisobj.deadfinish==true) {
                                exitflag=true;
                                break;
                        }
                }

                if(exitflag){
                        ReleaseAllDestroidResource();
                }
        }
        catch (e) {

        }
}

//获取当前敌人集合
export const GetEnemys=(objs:Obj_class[],myteam:number):Obj_class[]=>{
        let resaults:Obj_class[]=[];
        for(let i=0;i<objs.length;i++)
        {
                let tempobj:Basebio=objs[i].objval;
                if(tempobj.team!=myteam && tempobj.live>0 &&
                    tempobj.enable==true &&
                tempobj.isstart==true){

                        let tempclsobj=new Obj_class();
                        tempclsobj.objval=objs[i].objval;
                        tempclsobj.y=objs[i].y;
                        tempclsobj.x=objs[i].x;
                        tempclsobj.clsname=objs[i].clsname;
                        resaults.push(tempclsobj);
                }
        }

        let tempobj;
        for (let i = 0; i < resaults.length - 1; i++) {
                for (let j = 1; j < resaults.length - i - 1; j++) {
                        if (resaults[j - 1].x > resaults[j].x) {
                                tempobj = resaults[j - 1];
                                resaults[j - 1] = resaults[j];
                                resaults[j] = tempobj;
                        }
                }
        }
        return resaults;
}

